package com.itheima.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.service.LoadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Author chenry
 * @Package com.itheima.controller
 * @Date 2025/2/23 17:24
 */
@RestController
@RequestMapping("file")
public class EasyExcelFactoryController {

    private static final Logger logger = LoggerFactory.getLogger(EasyExcelFactoryController.class);
    @Resource
    private LoadService loadService;

    @PostMapping("/load")
    public String test(@RequestParam("file")MultipartFile file){

        long start = new Date().getTime();
        int successCount=0;
        int count=0;
        try {
            List<List<Object>> list = EasyExcel.read(file.getInputStream())
                                         .sheet()
                                         .doReadSync();
            count+=list.size();
            loadService.policyImport(list,successCount);
        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = new Date().getTime();
        logger.info("操作时间为:{},成功添加条数:{},失败条数:{}",(end-start),successCount,(count-successCount));
        return "成功！";
    }
}
